package com.huike.game.Activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.base.Game18;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import androidx.appcompat.app.AppCompatActivity;

public class GameActivity65 extends AppCompatActivity implements View.OnClickListener {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private EditText et_game65_input;
    private Button but_game65_click;
    private RequestQueue queue;
    private ProgressDialog show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game65);
        queue = Volley.newRequestQueue(this);
        initView();
    }


    private void show(){
        show = ProgressDialog.show(GameActivity65.this, "提示", "数据加载中...");
        show.show();
    }
    private void  hide(){
        show.dismiss();
    }

    private void initData(){

        String val = et_game65_input.getText().toString().trim();

        if(TextUtils.isEmpty(val)){
            Toast.makeText(this, "请输入需要查询的车量", Toast.LENGTH_SHORT).show();
            return;
        }

        show();

        JSONObject object = new JSONObject();
        try {
            object.put("carNumber",val);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "carViolation", object.toString(), new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                hide();

                try {
                    JSONObject data = jsonObject.getJSONObject("data");
                    String state = data.getString("state");
                    String msg = data.getString("msg");

                    if(state != null || state.equals("0") || state.equals(0)){
                        Toast.makeText(GameActivity65.this, msg, Toast.LENGTH_SHORT).show();
                        return;
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Game18 json = new Gson().fromJson(jsonObject.toString(), Game18.class);

                //使用Eventbus 传递数据
                EventBus.getDefault().postSticky(json);//使用粘性事件传送
                //跳转页面
                startActivity(new Intent(GameActivity65.this,GameActivityDesc65.class));


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                hide();
                Toast.makeText(GameActivity65.this, "网络异常！", Toast.LENGTH_SHORT).show();
            }
        });

        queue.add(request);

    }


    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);

        iv_rigth_image.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);
        tv_content_title.setText("车辆违章");
        tv_left_text.setVisibility(View.GONE);
        iv_left_image.setImageResource(R.drawable.tiao);

        et_game65_input = (EditText) findViewById(R.id.et_game65_input);
        but_game65_click = (Button) findViewById(R.id.but_game65_click);
        but_game65_click.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.but_game65_click:
                initData();
                break;
        }
    }

}
